package com.yc.bbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.yc.bbs.dao.TblTopicMapper;
import com.yc.bbs.entity.Result;
import com.yc.bbs.entity.TblTopic;
import com.yc.bbs.entity.TblUser;
import com.yc.bbs.service.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * BBS论坛-帖子表-19 前端控制器
 * </p>
 *
 * @author k
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/tblTopic")
public class TblTopicController {
    @Autowired
    TblTopicMapper topicMapper;
    @RequestMapping("queryByBid")
    public Page<TblTopic> queryByBid(int bid,int page,int rows){
        Page<TblTopic> p=new PageDTO<>(page,rows);
//        LambdaQueryWrapper<TblTopic> lqw=new LambdaQueryWrapper<>();
//        lqw.eq(TblTopic::getBoardid,bid);
        return topicMapper.selectByBid(bid,p);
    }

    @RequestMapping("queryById")
    public TblTopic queryById(int id){
        return topicMapper.queryById(id);
    }
    @PostMapping("post")
    public Result post(@RequestBody @Valid TblTopic topic, Errors errors, @SessionAttribute TblUser myinfo) throws BizException {
        if(errors.hasErrors()){
            //TODO
            throw new BizException("验证错误",errors.getAllErrors());
        }
        topic.setUid(myinfo.getId());
        topicMapper.insert(topic);
        return new Result(1,"发帖成功",null);
    }
}
